// pages/charge/chargeQuery.js
const cache = require("../../utils/cache")
const api = require("../../utils/api")
const util = require("../../utils/util")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    dateStart: '',
    dateEnd: '',
    currentPatient: '', // 当前就诊人
    mainList: [], //待交费
    mainListA: [] //已缴费
  },
  handleQuery(){
    this.queryPrescription()
  },
  queryPrescription() {
    if (this.data.currentPatient == null || this.data.currentPatient == '' || this.data.currentPatient == undefined) {
      return
    }
    let params = {}
    if (this.data.TabCur == 0) { // 待交费
      params.status = '0'
      params.startDt = this.data.dateStart + " 00:00:00"
      params.endDt = this.data.dateEnd + " 23:59:59"
    } else { //已缴费
      params.status = '1'
      params.startDt = this.data.dateStart + " 00:00:00"
      params.endDt = this.data.dateEnd + " 23:59:59"
    }
    params.ioNo = this.data.currentPatient.ioNo
    let that = this
    if (that.data.TabCur == 0) {
      api.getMainDtoList(params).then(res => {
        that.data.mainList.splice(0, that.data.mainList.length);
        if (res == null || res == undefined || res == '' || res.length == 0) {
          return;
        }
        let index = 0
        res.forEach(m => {
          m.prescriptionTime = util.timestampToDate(m.prescriptionTime)
          m.mainId = index++
        })
        that.data.mainList = res
        that.setData({
          mainList: that.data.mainList
        })
      })
    } else{
      api.getMainDtoList(params).then(res => {
        that.data.mainListA.splice(0, that.data.mainListA.length);
        if (res == null || res == undefined || res == '' || res.length == 0) {
          return;
        }
        res.forEach(m => {
          m.prescriptionTime = util.timestampToDate(m.prescriptionTime)
        })
        that.data.mainListA = res
        that.setData({
          mainListA: that.data.mainListA
        })
      })
    }
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
    })
    this.queryPrescription()
  },
  handleStartDateChange(e) {
    this.setData({
      dateStart: e.detail.value
    })
  },
  callbackLoad(e) {
    this.loadPatient();
  },
  loadPatient() {
    let cachePt = cache.currentPatient();
    if (cachePt != undefined && cachePt != null) {
      this.setData({
        currentPatient: cachePt
      })
    }
  },
  handleEndDateChange(e) {
    this.setData({
      dateEnd: e.detail.value
    })
  },
  handleChargeDetailClick(e) {
    let main = []
    if(this.data.TabCur==0){
        main= this.data.mainList.find(m=>m.mainId==e.currentTarget.dataset.id)
    }else{
        main = this.data.mainListA.find(m=>m.mainId==e.currentTarget.dataset.id)
    }
    if(main==undefined|| main==null||main.length==0){
      return
    }
    wx.navigateTo({
      url: '/pages/charge/prescriptionQuery?charge='+(this.data.TabCur==0?"1":"0"),
      success: function(res){
        res.eventChannel.emit('main',main)
      }
    })
  },
  showModal(event) {
    this.setData({
      modalName: event.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadPatient();

    let dateStart = new Date();
    let dateEnd = new Date();
    //时间
    dateStart = dateStart.setDate(dateStart.getDate() - 3);
    dateStart = new Date(dateStart);
    this.data.dateStart = util.formatDate(dateStart)
    this.data.dateEnd = util.formatDate(dateEnd)

    this.setData({
      dateStart: this.data.dateStart,
      dateEnd: this.data.dateEnd
    })

    this.queryPrescription()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this.selectComponent("#card")
    that.callbackFunc(() => that.loadData())
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})